<script>
var config = require('./config.js');
var http = require('./utils/httpHelper.js');
export default {
    data() {
        return {};
    },
    globalData: {
        withdrawalStateid:0,
        userInfo: null,

        getUserInfo: function (cb) {
            var that = this;
            if (this.userInfo != null) {
                if (typeof cb == 'function') {
                    cb(this.userInfo);
                }
            } else {
                //调用登录接口
                uni.login({
                    success: function (resLogin) {
                        if (resLogin.code) {
                            var code = resLogin.code;
                            uni.getUserInfo({
                                success: function (resUser) {
                                    that.userInfo = resUser.userInfo;
                                    var data = {
                                        code: code
                                    };
                                    http.httpGet(config.clubApi.putOpenid, data, function (resOpen, err) {
                                        if (resOpen.data.userId) {
                                            that.setUserInfo(resOpen.data);
                                        } else {
                                            let openId = resOpen.data.openid;
                                            that.queryUser(openId);
                                        }
                                    });
                                    if (typeof cb == 'function') {
                                        cb(that.userInfo);
                                    }
                                }
                            });
                        } else {
                            console.log('获取用户登录态失败！' + res.errMsg);
                        }
                    }
                });
            }
        },

        queryUser: function (openId) {
            var that = this;
            http.httpGet(
                config.clubApi.wxUser,
                {
                    openId: openId
                },
                function (res) {
                    if (res.data) {
                        that.setUserInfo(res.data);
                    } else {
                        uni.setStorageSync('user', null);
                        uni.setStorageSync('address', null);
                    }
                }
            );
        },

        setUserInfo(obj) {
            var that = this;
            that.userInfo = {};
            that.userInfo.userId = obj.userId;
            that.userInfo.userName = obj.userName;
            that.userInfo.avatar = obj.avatar;
            that.userInfo.phonenumber = obj.phonenumber;
            that.userInfo.sex = obj.sex;
            that.userInfo.token = obj.token;
            uni.setStorageSync('user', that.userInfo);
            uni.setStorageSync('address', obj.allAddress);
        }
    }
};
</script>
<style lang="scss">
@import './app.scss';
</style>
